// pages/show_file_search/show_file_search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShade:false,
    animationData: {},
    showDownloadCard:false,
    selectedFile:null,
    score:0,
    index:0,
  },
  translate: function (event) {
    let app = getApp()
    this.setData({showShade:true})
    let height = wx.getSystemInfoSync().windowHeight;
    this.setData({showDownloadCard:true})
    this.animation.translateY(-height * 0.54).step()
    this.setData({animation: this.animation.export()})

    let index = event.currentTarget.dataset.index
    let type = event.currentTarget.dataset.type
    if(type == "selected"){
      this.setData({
        selectedFile :app.globalData.searchFileList[this.data.index]
      })
    }else{
      this.setData({
        selectedFile : this.data.otherSearchList[index]
      })
    }

    let score = app.globalData.userInfo.score
    let neededScore = this.data.selectedFile.score
    let selectedFile = this.data.selectedFile
    this.setData({score:score})
    if(score < neededScore){
      selectedFile.panUrl = "积分不足，赶快做题赚取积分吧。"
      this.setData({selectedFile:selectedFile})
      
    }

  },
  
  hide:function(){
    let height = wx.getSystemInfoSync().windowHeight;
    this.setData({showShade:false})
    this.animation.translateY(height * 0.54).step()
    this.setData({animation: this.animation.export()})
    setTimeout(() => {
      this.setData({showDownloadCard:false})
    }, 500)

    
  },
  copyLink:function(){
    
    let app = getApp()
    let score = app.globalData.userInfo.score
    let neededScore = this.data.selectedFile.score
    if(score < neededScore){
      wx.showToast({
        title: '积分不足',
        icon: 'none',
        duration: 1000
      })      
    }else{
      score = score - neededScore
      app.globalData.userInfo.score = score
      wx.setClipboardData({
        data: this.data.selectedFile.panUrl,
        success :res => {
          wx.getClipboardData({
            success (res) {
              console.log(res.data) // data
            }
          })
          wx.showToast({
            title: '拷贝成功',
            icon: 'success',
            duration: 1000
          })    
          this.setData({showDownloadCard:false})
          this.setData({showShade:false})
          wx.request({
            url: app.globalData.url + '/user/updateselective',
            data: {
              score: app.globalData.userInfo.score,
              openId:app.globalData.userInfo.openid,
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: result => {
              console.log(result.data)
            }
          })
        }
      })
      
    }
  },
  mark:function(){
    wx.showToast({
      title: '标记成功',
      icon: 'success',
      duration: 1000
    })
  },

  onLoad: function (options) {
    let app = getApp()
    let selectedItem = app.globalData.selectedSearchFile
    let index = selectedItem.index
    this.setData({
      index:index,
      selectedFile: selectedItem.item
    })
    let otherSearchList = new Array()
    let searchFileList = app.globalData.searchFileList
    for(let i = 0; i < searchFileList.length; ++i){
      if(i != index){
        otherSearchList.push(searchFileList[i])
      }
    }
    this.setData({
      otherSearchList:otherSearchList,
    })
  },
  onReady: function () {
    this.animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})